var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
var commonUtil = require("../../../utils/commonUtil.js")
Page({

  data: {
    imgUrl: globalData.imgUrl,
    list:[],
    pageNo:1,
    pageSize:10,
    totalPage: 1,//总页数
    orderId:'',
  },
  list: function () {
    var params = new Object();
    params.pageNo = this.data.pageNo;
    params.pageSize = this.data.pageSize;
    var that = this;
    let openId = wx.getStorageSync("openidKey");
    console.log("param:" + JSON.stringify(params));
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/card/getCardList",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("res：" + JSON.stringify(res));
        if (res.result == 0) {
          let rows = res.data.list;
          that.setData({ totalPage: res.data.totalPage })
          if (that.data.pageNo == 1) {
            that.setData({ list: rows })
          } else {
            var list = that.data.list;
            list = list.concat(rows);
            that.setData({ list: list })
          }
        }
      }
    })
  },
  //提交订单
  submitOrder: function (e) {
    var that = this;
    let total = this.data.total;
    let params = new Object();
    params.cardNo = e.currentTarget.id;
    console.log("卡订单提交参数" + JSON.stringify(params));
    commonUtil.showLoading("提交中...");
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId},
      url: httpsUrl + "/order/subBuyCard",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        commonUtil.hideLoading();
        console.log("提交订单返回:" + JSON.stringify(res));
        res = res.data;
        if (res.result == 0) {
          that.setData({ orderId: res.data })
          that.goPay();
          //that.paySuccess();
        } else {
          commonUtil.showModal(res.msg);
        }
      }
    })
  },
  //获取openid
  goPay: function () {
      var that = this;
      wx.login({
        success: function (res) {
          wx.showLoading({ title: '请稍等...', })
          wx.request({
            header: { "content-Type": "application/x-www-form-urlencoded" },
            url: httpsUrl + "/wechat/getOpenId",
            dataType: "json",
            method: "POST",
            data: { "code": res.code, "agentCode": globalData.agentCode },
            success: function (res) {
              wx.hideLoading();
              res = res.data;
              console.log("获取openid返回:" + JSON.stringify(res));
              if (res.result == 0) {
                let data = JSON.parse(res.data);
                console.log(data.openid);
                that.setData({ openid: data.openid })
                that.pay();
              } else {
                console.log("获取openid失败：" + res.msg);
              }
            }
          })
        }
      })
  },
  //支付
  pay: function () {
    let that = this;
    let params = new Object();
    params.orderNo = this.data.orderId;
    params.openid = this.data.openid;
    console.log(JSON.stringify(params));
    commonUtil.showLoading("去支付...");
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/order/payParam",//统一下单返回支付参数
      dataType: "json",
      method: "POST",
      data: params,
      success: function (res) {
        commonUtil.hideLoading();
        console.log("统一下单返回" + JSON.stringify(res));
        res = res.data;
        if (res.result == 0) {
          var data = res.data;
          //调起支付
          wx.requestPayment(
            {
              'timeStamp': data.timeStamp,
              'nonceStr': data.nonceStr,
              'package': data.package,
              'signType': 'MD5',
              'paySign': data.paySign,
              'success': function (res) {
                console.log("支付调起成功");
                that.paySuccess();
              },
              'fail': function (res) {
                console.log("用户取消支付");
                // wx.switchTab({
                //   url: '../../order/orderList/orderList',
                // })
              },
              'complete': function (res) { }
            })
        } else {
          commonUtil.showModal("支付失败：" + res.msg);
        }
      }
    })
  },

  paySuccess: function () {
    wx.navigateTo({
      url: '../../order/paySuccess/paySuccess',
    })
  },
  memberCardShop:function(e){
    let id = e.currentTarget.id;
    wx.setStorageSync("cardId", id)
    wx.navigateTo({
      url: '../memberCardShop/memberCardShop',
    })
  },
  content:function(){
    wx.setStorageSync("contentsType", 2);
    wx.navigateTo({
      url: '../contents/contents',
    })
  },
  memberCardInfo:function(e){
    let id = e.currentTarget.id;
    wx.setStorageSync("cardId", id)
    wx.navigateTo({
      url: '../memberCardInfo/memberCardInfo',
    })
  },
  memberCardUseNote: function () {
    wx.navigateTo({
      url: '../memberCardUseNote/memberCardUseNote',
    })
  },

  onLoad: function (options) {
    this.list();
  },

  onShow: function () {

  },
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    let pageNo = this.data.pageNo;
    pageNo = pageNo + 1;
    if (pageNo > this.data.totalPage) {
      commonUtil.showSuccessToast("没有更多啦~");
      return;
    } else {
      this.setData({ pageNo: pageNo })
      this.list();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})